<template>
	<view class="equipment">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				设备下拨
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}">
			<view class="top clearfix">
				<view class="left fl clearfix">
					<view class="mess fl">
						<view :class="[attrIndex==1?'active':'']" @click="attrClick(1)">选中下拨</view>
						<text class="dian" v-if="attrIndex == 1"></text>
					</view>
					<view class="mess mess1 fl" @click="attrClick(2)">
						<view :class="[attrIndex==2?'active':'']">扫码下拨</view>
						<text class="dian" v-if="attrIndex == 2"></text>
					</view>
				</view>
				<view class="right fr" @click="topage('/subpackageA/equipment/xiaborecord')">
					<view class="name">下拨记录</view>
				</view>
			</view>
			<view class="center" v-if="attrIndex == 1">
				<view class="sousuo">
					<input type="text" placeholder="请输入关键字" v-model="keywords" />
					<image :src="baseUrl + '/wximage/sousuo.png'"></image>
				</view>
			</view>
			<view class="center" v-if="attrIndex == 2">
				<view class="sousuo">
					<input type="text" placeholder="请输入设备SN码" v-model="keywords" />
					<view class="iconfont icon-iconfontscan" @click="scanCode"></view>
				</view>
			</view>
		</view>
		<view class="mendian" v-if="attrIndex == 1">
			<view class="heads clearfix">
				<view class="left fl">
					<image :src="baseUrl + '/wximage/hktubiao.png'"></image>
					<view class="name">库存{{deviceList.length}}台</view>
				</view>
				<view class="right fr" @click="allcheck()">
					<view class="name">全选</view>
				</view>
			</view>
			<view class="shuju" v-if="deviceList.length != 0">
				<view class="li" v-for="(item,index) in deviceList" :key="index" @click="dianji(item,index)" >
					<view class="cen">
						<text class="title">设备SN：{{item.charger_number}}</text>
						<text class="time">入库时间：{{item.created_at}}</text>
					</view>
					<image v-if="item.checked==false" :src="baseUrl + '/wximage/weixz.png'"></image>
					<image v-else :src="baseUrl + '/wximage/yixz.png'" mode=""></image>
				</view>
				
				<!-- <view class="you" v-if="item.checked==false"><image :src="baseUrl + '/wximage/weixz.png'"></image></view>
				<view class="you" v-else><image :src="baseUrl + '/wximage/yixz.png'"></image></view> -->
				
				<!-- <view class="li">
					<view class="cen">
						<text class="title">设备SN：8363837363387</text>
						<text class="time">入库时间：2024-07-12 12:14:25</text>
					</view>
					<image :src="baseUrl + '/wximage/yixz.png'"></image>
				</view> -->
			</view>
			<view class="empty" v-else>
				<image :src="baseUrl + '/wximage/empty.png'"></image>
				<text>暂无数据</text>
			</view>
		</view>
		<view class="mendian" v-if="attrIndex == 2">
			<view class="heads clearfix">
				<view class="left fl">
					<image :src="baseUrl + '/wximage/hktubiao.png'"></image>
					<view class="name">已添加设备{{anzhuang1}}台</view>
				</view>
				<view class="right fr" @click="allcheck2()">
					<view class="name">全选</view>
				</view>
			</view>
			
			<view class="shuju" v-if="deviceList2.length != 0">
				<view class="li" v-for="(item,index) in deviceList2" :key="index" @click="dianji1(item,index)">
					<view class="cen">
						<text class="title">设备SN：{{item.charger_number}}</text>
						<text class="time">入库时间：{{item.created_at}}</text>
					</view>
					<image v-if="item.checked==false" :src="baseUrl + '/wximage/weixz.png'"></image>
					<image v-else :src="baseUrl + '/wximage/yixz.png'" mode=""></image>
				</view>
			</view>
			<view class="empty" v-else>
				<image :src="baseUrl + '/wximage/empty.png'"></image>
				<text>暂无数据</text>
			</view>
			
		</view>
		<view class="footer clearfix">
			<view class="name fl">已选中：{{checknum}}台</view>
			<view class="submit fr" @click="submit">确定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				keywords: '',
				list: [1,1,2,1,1,1],
				attrIndex: 1,
				formData: {
					bussinessid:uni.getStorageSync('agent_id'),
					page:1,
					pageSize:30,
					search:'',
					type:1
				},
				deviceList:[],
				deviceList2:[],
				checkarr:[],
				checknum:0,
				attrIndex:1,
				anzhuang1:0
			}
		},
		onLoad() {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			this.getDeviceList();
		},
		methods: {
			attrClick(item){
				this.attrIndex = item;
				
				
				
				this.deviceList=[];
				this.deviceList2=[];
				this.checkarr=[];
				this.checknum=0;
				this.formData.page=1;
				this.formData.search='';
				
				if(this.attrIndex==1){
					this.getDeviceList();
				}
			},
			//全选
			allcheck(){
				// console.log("全选中")
				this.checkarr = [];
				for(let i=0;i<this.deviceList.length;i++){
					this.deviceList[i].checked = true;
					
					this.checkarr.push({id: item.id,sn:item.charger_number});

				}
				this.checknum = this.deviceList.length;
			},
			
			allcheck2(){
				// console.log("全选中")
				this.checkarr = [];
				for(let i=0;i<this.deviceList2.length;i++){
					this.deviceList2[i].checked = true;
					
					this.checkarr.push({id: item.id,sn:item.charger_number});
				
				}
				this.checknum = this.deviceList2.length;
			},
			//设备的sn码
			dianji(item,key){
				//查看是否选中 如果是false变为true 并存储到数组
				if(item.checked == false){
					this.deviceList[key].checked = true;
					this.checknum = this.checknum+1;
					this.checkarr.push({id: item.id,sn:item.charger_number});
				}else{
					this.deviceList[key].checked = false;
					this.checknum = this.checknum-1;
					for(let i=0;i<this.checkarr.length;i++){
						if(this.checkarr[i].id == item.id){
							this.checkarr.splice(i);
						}
					}
				}
				console.log(this.checkarr)
				
			},
			dianji1(item,key){
				//查看是否选中 如果是false变为true 并存储到数组
				if(item.checked == false){
					this.deviceList2[key].checked = true;
					this.checknum = this.checknum+1;
					this.checkarr.push({id: item.id,sn:item.charger_number});
				}else{
					this.deviceList2[key].checked = false;
					this.checknum = this.checknum-1;
					for(let i=0;i<this.checkarr.length;i++){
						if(this.checkarr[i].id == item.id){
							this.checkarr.splice(i);
						}
					}
				}
				console.log(this.checkarr)
			},
			
			getDeviceList(){
				if(this.attrIndex == 1){
					// this.formData.type = 1;
					this.request({
						url: '/wxdevice/deviceallocated',
						data: this.formData,
						method: "POST"
					}).then(res => {
						if (res.data.code == 200) {
							// console.log(res);
							if(res.data.data.list.length!=0){
								this.deviceList = res.data.data.list;
								// this.bussinessList = this.bussinessList.concat(res.data.data);
							}
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					});
				}else{
					// this.formData.type = 2;
					
				}
				
			},
			scanCode() {
				uni.scanCode({
					success: (res) => {
						if(res.result){
							let url = res.result;
							
							let qrcodeurl = getApp().globalData.baseUrl;
							
							if(url.indexOf('/scan') > -1){
								var arrUrl = url.split('qrcodenumber=');
								// console.log(arrUrl[1]);
								var qrcode = arrUrl[1];
								var formData = {
									'qrcode': qrcode,
									'agentid': uni.getStorageSync('agent_id')
								}
								//请求接口回收设别
								this.request({
									url: '/wxdevice/assignmerchants',
									data: formData,
									method: "POST"
								}).then(res => {
									// console.log("打印回收返回值")
									// console.log(res)
									if (res.data.code == 200) {
										// var stores_id = res.data.data.id;//用户id
										this.deviceList2.push(res.data.data);
										++this.anzhuang1;
										uni.showToast({
											title: '添加成功',
											icon: 'none'
										});
										
										setTimeout(function(){
											uni.reLaunch({
												url: '/subpackageA/index/index'
											})
										},2000)
									} else {
										uni.showToast({
											title: res.data.message,
											icon: 'none'
										});
									}
								})
								
							}
						}
						
					},
					fail: (res) => {
						console.log("未识别到二维码");
					}
					
				})
			},
			
			submit(){
				uni.navigateTo({
					url: '/subpackageA/equipment/xiaboqueren?checkarr='+JSON.stringify(this.checkarr)+'&checknum='+this.checknum+'&attr='+this.attrIndex
				})
			},
			topage(url){
				uni.navigateTo({
					url
				})
			},
			fanhui() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.equipment {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			padding: 20rpx 40rpx 0;
			box-sizing: border-box;
		
			.top{
				.left {
					.mess {
						height: 48rpx;
						line-height: 48rpx;
						text-align: center;
			
						view {
							font-size: 24rpx;
							color: #333333;
						}
			
						.dian {
							width: 8rpx;
							height: 8rpx;
							background: #5C88FA;
							display: block;
							border-radius: 50%;
							margin: 0 auto;
						}
			
						.active {
							font-size: 32rpx;
							color: #333333;
							font-weight: 700;
							margin-left: 0;
						}
					}
			
					.mess1 {
						margin-left: 20rpx;
					}
				}
			
				.right {
					width: 124rpx;
					height: 36rpx;
					line-height: 36rpx;
					text-align: center;
					background: #FFFFFF;
					border-radius: 24rpx;
					margin-top: 6rpx;
			
					.name {
						font-size: 24rpx;
						color: #333333;
					}
				}
			}
			
			.center{
				margin-top: 30rpx;
				
				.sousuo {
					width: 100%;
					box-sizing: border-box;
					position: relative;
					input {
						width: 100%;
						height: 60rpx;
						line-height: 60rpx;
						background: #ffffff;
						border-radius: 30rpx;
						padding-left: 30rpx;
						color: #999999;
						box-sizing: border-box;
					}
						
					image {
						width: 72rpx;
						height: 40rpx;
						position: absolute;
						right: 10rpx;
						top: 10rpx;
						z-index: 100;
					}
					
					.iconfont {
						font-size: 40rpx;
						position: absolute;
						right: 24rpx;
						top: 10rpx;
						color: #5C88FA;
						z-index: 100;
					}
				}
			}
		}
		
		.mendian{
			width: 90%;
			margin: 0 auto;
			z-index: 70;
			margin-top: -110rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-shadow: rgba(223, 230, 255, 0.67) 0px 0px 6px;
			margin-bottom: 120rpx;
			
			.heads{
				width: 100%;
				padding: 24rpx 20rpx 0;
				box-sizing: border-box;
				
				.left{
					
					image{
						float: left;
						width: 40rpx;
						height: 40rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 28rpx;
						color: #333333;
						float: left;
					}
				}
				
				.right{
					
					image{
						float: left;
						width: 40rpx;
						height: 40rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 28rpx;
						color: #368BF8;
						float: left;
					}
				}
			}
			
			.shuju{
				width: 100%;
				padding: 30rpx 26rpx;
				box-sizing: border-box;
				
				.li{
					width: 100%;
					position: relative;
					padding: 30rpx;
					margin-bottom: 20rpx;
					background: #F6F8FF;
					border-radius: 16rpx;
					
					image{
						width: 44rpx;
						height: 44rpx;
						position: absolute;
						top: 46rpx;
						right: 30rpx;
					}
					
					.cen{
						width: 480rpx;
						
						text{
							display: block;
						}
						
						.title{
							font-size: 28rpx;
							color: #333333;
							margin-bottom: 6rpx;
						}
						
						.time{
							font-size: 24rpx;
							color: #999999;
						}
					}
				}
			}
		}
		
		.empty{
			width: 100%;
			margin: 20rpx auto 0;
			text-align: center;
			padding-bottom: 20rpx;
			
			image{
				width: 390rpx;
				height: 340rpx;
			}
			
			text{
				display: block;
				margin-top: 30rpx;
				font-size: 24rpx;
				color: #666666;
			}
		}
		
		.footer{
			width: 100%;
			margin: 0 auto;
			height: 72rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			padding: 0 60rpx;
			box-sizing: border-box;
			
			.name{
				height: 72rpx;
				line-height: 72rpx;
				font-size: 32rpx;
				color: #333333;
			}
			
			.submit{
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #FFFFFF;
				font-size: 32rpx;
				text-align: center;
			}
		}
	}
</style>